package com.gopay.bis.ceb.expay.operation;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.common.util.TransUtil;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitNSOQRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitNSQRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitPPRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitQSCRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitQUSRes;
import com.gopay.common.domain.bank.ceb.expay.res.msg.MessageSuitQPRes;
import com.gopay.common.domain.bank.ceb.expay.res.msg.MessageSuitUSCRes;
import com.thoughtworks.xstream.XStream;

/**
 * 
 * 
 * @author lilili
 * @CebExPayParseData.java
 * 
 */
public class CebExPayParseData {

    protected Log log = LogFactory.getLog(this.getClass());

    public Object parseUscRes(Object messageData) {
        MessageSuitUSCRes messageSuit = new MessageSuitUSCRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }

        if (retString.indexOf("errorCode") == -1) {
            XStream xstream = new XStream();
            xstream.autodetectAnnotations(true);
            retString = retString.replaceAll("ds:", "");
            xstream.processAnnotations(MessageSuitUSCRes.class);
            messageSuit = (MessageSuitUSCRes) xstream.fromXML(retString);
        }
        log.info("parse   message :" + messageSuit);
        return messageSuit;
    }

    public Object parseQSCRes(Object messageData) {
        CebExpaySuitQSCRes messageSuit = new CebExpaySuitQSCRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        retString = retString.replaceAll("ds:", "");
        xstream.processAnnotations(CebExpaySuitQSCRes.class);
        messageSuit = (CebExpaySuitQSCRes) xstream.fromXML(retString);
        log.info("parse   message :" + messageSuit);
        return messageSuit;
    }

    /**
     * 解析签约响应结果
     * 
     * @param messageData
     * @return
     * @author w_jiqing
     */
    public Object parseQUSRes(Object messageData) {
        CebExpaySuitQUSRes messageSuit = new CebExpaySuitQUSRes();
        String retString = null;

        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        // if (retString.indexOf("errorCode") == -1) {
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        retString = retString.replaceAll("ds:", "");
        xstream.processAnnotations(CebExpaySuitQUSRes.class);
        messageSuit = (CebExpaySuitQUSRes) xstream.fromXML(retString);
        // }
        log.info("parse   message :" + messageSuit);
        return messageSuit;
    }

    public Object parseQpRes(Object messageData) {
        MessageSuitQPRes messageSuit = new MessageSuitQPRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        if (retString.indexOf("errorCode") == -1) {
            XStream xstream = new XStream();
            xstream.autodetectAnnotations(true);
            xstream.processAnnotations(MessageSuitQPRes.class);
            retString = retString.replaceAll("ds:", "");
            messageSuit = (MessageSuitQPRes) xstream.fromXML(retString);
        } else {
            messageSuit.setReturnInfo(TransUtil.getMsgField("<errorMessage>", "</errorMessage>", retString));
            messageSuit.setBankRetCode(TransUtil.getMsgField("<errorCode>", "</errorCode>", retString));
        }
        return messageSuit;
    }

    public Object parsePPRes(Object messageData) {
        CebExpaySuitPPRes messageSuit = new CebExpaySuitPPRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(CebExpaySuitPPRes.class);
        retString = retString.replaceAll("ds:", "");
        messageSuit = (CebExpaySuitPPRes) xstream.fromXML(retString);
        // 记录数据库失败信息 add by zhengxue 20170313
        if (retString.indexOf("errorCode") != -1) {
            messageSuit.setReturnInfo(TransUtil.getMsgField("<errorMessage>", "</errorMessage>", retString));
            messageSuit.setBankRetCode(TransUtil.getMsgField("<errorCode>", "</errorCode>", retString));
        }
        return messageSuit;

    }

    /**
     * 解析签约查询响应结果
     * 
     * @param messageData
     * @return
     * @author w_jiqing
     */
    public Object parseNSQRes(Object messageData) {
        CebExpaySuitNSQRes messageSuit = new CebExpaySuitNSQRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        if (retString.indexOf("errorCode") == -1) {
            XStream xstream = new XStream();
            xstream.autodetectAnnotations(true);
            retString = retString.replaceAll("ds:", "");
            xstream.processAnnotations(CebExpaySuitNSQRes.class);
            messageSuit = (CebExpaySuitNSQRes) xstream.fromXML(retString);
        }
        log.info("parse   message :" + messageSuit);
        return messageSuit;

    }

    public Object parseSoqRes(Object messageData) {
        CebExpaySuitNSOQRes messageSuit = new CebExpaySuitNSOQRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(CebExpaySuitNSOQRes.class);
        retString = retString.replaceAll("ds:", "");
        messageSuit = (CebExpaySuitNSOQRes) xstream.fromXML(retString);
        if (retString.indexOf("errorCode") != -1) {
            messageSuit.setBankRetCode(TransUtil.getMsgField("<errorCode>", "</errorCode>", retString));
        }
        return messageSuit;
    }

    public Object parseSooqRes(Object messageData) {
        CebExpaySuitNSOQRes messageSuit = new CebExpaySuitNSOQRes();
        String retString = null;
        if (messageData instanceof byte[]) {
            retString = new String((byte[]) messageData);
        } else if (messageData instanceof String) {
            retString = (String) messageData;
        }
        if (retString.indexOf("errorCode") != -1) {
            messageSuit.setBankRetCode(TransUtil.getMsgField("<errorCode>", "</errorCode>", retString));
        }
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(CebExpaySuitNSOQRes.class);
        retString = retString.replaceAll("ds:", "");
        messageSuit = (CebExpaySuitNSOQRes) xstream.fromXML(retString);
        return messageSuit;
    }

}
